<template>
  <div>
    <div style="margin: 10px 0;">
      <el-row type="flex" class="row-search" >
        <el-col :span="2"><el-button @click="toggleSelection()" style="margin-left: 10px;">取消全选</el-button></el-col>
        <el-col :span="4"><el-input v-model="menuName" style="float:left; margin-left: 10px;  width: 200px;" suffix-icon="el-icon-search" placeholder="请输入菜单名："></el-input></el-col>
        <el-col :span="2"><el-button type="primary" style="margin-left: 10px;" icon="el-icon-search" @click="load">搜索</el-button></el-col>
        <el-col :span="1"><el-button type="info"  icon="el-icon-refresh-left" @click="reset">重置</el-button></el-col>
      </el-row>
    </div>

    <div style="margin: 10px 0; ">
      <el-row type="flex" class="row-update" >
        <el-col :span="2"><el-button type="success"  size="medium" style=" margin: 10px;" icon="el-icon-circle-plus-outline" @click="addMenu">新增</el-button></el-col>
        <el-popconfirm
            style="margin-left: 5px;"
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="您确定执行批量删除吗？"
            @confirm="bathDel()"
        >
          <el-button slot="reference" type="danger"   size="medium" style="margin: 10px;" icon="el-icon-delete" > 批量删除</el-button>
        </el-popconfirm>
<!--        <el-col :span="2">
          <el-upload>
            <el-button action='http://localhost:9090/user/import'  type="primary"  size="medium" style="margin: 10px;" icon="el-icon-bottom"> 导入</el-button>
          </el-upload>
        </el-col>-->
<!--        <el-col :span="2"><el-button type="primary"  size="medium" style="margin: 10px;" icon="el-icon-top" @click="exp"> 导出</el-button></el-col>-->
      </el-row>
    </div>

    <el-table
        :header-cell-style="{ background: '#F2F2F2', color: '#333' }"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="菜单ID"  show-overflow-tooltip/>
      <el-table-column prop="menuName" label="菜单名称"  show-overflow-tooltip/>
      <el-table-column prop="menuPath" label="路径" />
      <el-table-column prop="pagePath" label="页面路由路径" />
      <!--插槽显示该行的icon值，-->
      <el-table-column prop="icon" label="图标"  align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="font-size: 20px;"></i>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" />
      <el-table-column prop="statue" label="状态" />
<!--      <el-table-column prop="pid" label="父节点" />-->
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleAdd(scope.row.id)" v-if="!scope.row.pid && !scope.row.menuPath" icon="el-icon-circle-plus" size="mini">新增子菜单</el-button>
          <el-button type="primary" @click="handleEdit(scope.row)" icon="el-icon-edit" size="mini">修改</el-button>
          <el-popconfirm
              style="margin-left: 5px;"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除该条消息吗？"
              @confirm="handleDel(scope.row.id)"
          >
            <el-button type="danger" slot="reference" icon="el-icon-delete" size="mini">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

<!--    <div style="padding: 10px;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>-->

    <el-dialog title="新增菜单" :visible.sync="dialogFormVisible" width="40%">
      <el-form label-width="100px">
        <el-form-item label="菜单名称" >
          <el-input v-model="form.menuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径" >
          <el-input v-model="form.menuPath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="页面路由路径" >
          <el-input v-model="form.pagePath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" >
          <el-select clearable  v-model="form.icon" placeholder="选择图标" style="width: 100%;" >
            <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value ">
              <i :class="item.value"/>  {{item.name}}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" >
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-row>
          <el-col :span="11"><el-button @click="dialogFormVisible = false">取 消</el-button></el-col>
          <el-col :span="5"><el-button type="primary" @click="save">确 定</el-button></el-col>
        </el-row>


      </div>
    </el-dialog>

    <el-dialog title="新增模块" :visible.sync="addFormVisible" width="30%">
      <el-form label-width="70px">
        <el-form-item label="菜单名称" >
          <el-input v-model="form.menuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父菜单" >
          <el-input v-model="form.pid" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
export default {
  name: "Menu",
  data(){
    return{
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 3,
      menuName: '',
      multipleSelection: [],
      collapseBtnClass: 'el-icon-s-unfold',
      input: '',
      dialogFormVisible: false,
      //上放新增键开启状态
      addFormVisible:false,
      form: {
      },
      formLabelWidth: '70px',
      options: []
    }
  },
  created() {
    this.load()
  },
  methods:{
    addMenu(){
        this.addFormVisible =true
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    load(){
      this.request.get("/menu",{
        params: {
          menuName: this.menuName,
        }
      }).then(res => {
       /* console.log(res)*/
        this.tableData = res.data
      })
    },
    reset(){
      //重置搜索框
      this.menuName = ""
      this.load()

    },
    handleAdd(id){
      //新增
      this.dialogFormVisible = true
      this.form = {}
      if(id){
        //设置父路由id
        this.form.pid=id;
      }

      this.request.get("/menu/icons",{
      }).then(res => {
        console.log(res)
        this.options = res.data
      })

      /*新增菜单入参打印*/
      /*console.log(this.form)*/
    },
    //保存
    save(){

      this.request.post("/menu/save",this.form)
          .then(res => {
            //若果返回结果为true
            if(res.data){
              this.$message.success("保存成功")
              this.dialogFormVisible = false
              this.addFormVisible = false
              this.load()
            }else {
              this.$message.error("保存失败")
            }
          })
    },
    //编辑
    handleEdit(row){
      this.form = row
      this.dialogFormVisible = true

      //请求图标数据
      this.request.get("/menu/icons",{
      }).then(res => {
        console.log(res)
        this.options = res.data
      })
    },
    handleDel(id){
      this.request.delete('/menu/' + id).then(res => {
        if(res.code === '200'){
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(this.multipleSelection)
      this.multipleSelection = val
    },
    //批量删除
    bathDel(){
      //map键值对绑定multipleSelection中的id值  [{},{},{}] => [multipleItem.1.id,..2.id,..3.id,..4]
      let ids = this.multipleSelection.map(v => v.id)
      this.request.post("/menu/bath/del",ids).then(res => {
        if(res.code === '200'){
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
      console.log(ids)
    },

    handleSizeChange(pageSize){
      this.pageSize = pageSize
      console.log(pageSize)
      this.load()
    },
    handleCurrentChange(pageNum){
      this.pageNum = pageNum
      console.log(pageNum)
      this.load()
      }/*,
      exp() {
        window.open("http://localhost:9090/user/export")
      }*/
  }

}
</script>

<style scoped>
.el-header {
  background-color: #ffffff;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>